home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 November / EnigmA AMIGA RUN 02 (1995)(G.R. Edizioni)(IT)[!][issue 1995-11][Skylink CD].iso / earcd / unix / mp14tar.z / mp14tar / mpack / amigaunpk.c < prev    next >
C/C++ Source or Header  |  1994-06-01  |  6KB  |  159 lines

  1. /* (C) Copyright 1993 by Mike W. Meyer
  2.  *
  3.  * Permission to use, copy, modify, distribute, and sell this software
  4.  * and its documentation for any purpose is hereby granted without
  5.  * fee, provided that the above copyright notice appear in all copies
  6.  * and that both that copyright notice and this permission notice
  7.  * appear in supporting documentation, and that the name of Mike W.
  8.  * Meyer not be used in advertising or publicity pertaining to
  9.  * distribution of the software without specific, written prior
  10.  * permission.  Mike W. Meyer makes no representations about the
  11.  * suitability of this software for any purpose.  It is provided "as
  12.  * is" without express or implied warranty.
  13.  *
  14.  * MIKE W. MEYER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  15.  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  16.  * FITNESS, IN NO EVENT SHALL MIKE W. MEYER BE LIABLE FOR ANY SPECIAL,
  17.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  18.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  19.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
  20.  * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21.  */
  22. #include <exec/types.h>
  23. #include <exec/execbase.h>
  24. #include <dos/dos.h>
  25. #include <libraries/netsupport.h>
  26.  
  27. #ifdef __SASC
  28. #include <proto/exec.h>
  29. #include <proto/dos.h>
  30. #include <proto/netsupport.h>
  31. #else
  32. #include <clib/exec_protos.h>
  33. #include <clib/dos_protos.h>
  34. #include <clib/netsupport_protos.h>
  35. #endif
  36.  
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <errno.h>
  41. #include "amiga_protos.h"
  42. #include "version.h"
  43.  
  44. #if defined(__SASC) && (__VERSION__ > 5) && (__REVISION__ > 50)
  45. static const char DOSId[] = "\0$VER: MPack " MPACK_VERSION " " __AMIGADATE__ ;
  46. #else
  47. static const char DOSId[] = "\0$VER: MPack " MPACK_VERSION " (" __DATE__ ")" ;
  48. #endif
  49.  
  50. int quiet ;
  51.  
  52. #define TEMPLATE        "Files/M,-f=Overwrite/S,-C=Directory/K,-q=Quiet/S,-t=Text/S"
  53. enum { FILES, OVERWRITE, DIRECTORY, QUIET, EXTRACTTEXT, OPT_COUNT } ;
  54. static struct RDArgs *args = NULL, *my_args = NULL ;
  55. BPTR    OldDir = NULL ;
  56. struct NetSupportLibrary *NetSupportBase;
  57.  
  58. #define HELPSTRING "munpack version " MPACK_VERSION "\n\
  59. Unpack input files. If no files are present, reads from standard in. The\n\
  60. arguments other than file names are:\n\
  61. -f=Overwrite/S          Causes the unpacked file to overwrite any existing\n\
  62.                         files of the same name. Otherwise, an extension\n\
  63.                         with a period and a number is added to the file\n\
  64.                         name.\n\
  65. -C=Directory/K          Change to the given directory before unpacking.\n\
  66.                         Path names will be interpreted relative to the this\n\
  67.                         directory, not the current one.\n\
  68. -q=Quiet/S              Supress progress statements.\n\
  69. -t=Text/S               Unpack the Text part fo multipart messages.\n"
  70.  
  71. void warn(char *) ;
  72. void chat(char *) ;
  73.  
  74. void
  75. FreeSystem(void) {
  76.  
  77.         if (NetSupportBase) {
  78.                 UnLockFiles() ;
  79.                 CloseLibrary((struct Library *) NetSupportBase) ;
  80.                 }
  81.         if (args) FreeArgs(args) ;
  82.         if (my_args) FreeDosObject(DOS_RDARGS, args) ;
  83.         if (OldDir) UnLock(CurrentDir(OldDir)) ;
  84.         }
  85.  
  86. main(int argc, char **argv) {
  87.         long opts[OPT_COUNT] ;
  88.         FILE *file ;
  89.         int goodenough, extracttext ;
  90.         extern struct ExecBase *SysBase ;
  91.  
  92.         NetSupportBase = (struct NetSupportLibrary *) OldOpenLibrary(NETSUPPORTNAME) ;
  93.         goodenough = SysBase->LibNode.lib_Version > 36 ;
  94.  
  95.         /* Do the 2.x argument parsing stuff */
  96.         if (!goodenough) opts[FILES] = argc ;
  97.         else {
  98.                 onexit(FreeSystem) ;
  99.                 memset((char *) opts, 0, sizeof(opts)) ;
  100.                 if (!(my_args = AllocDosObject(DOS_RDARGS, NULL))) {
  101.                         PrintFault(IoErr(), *argv) ;
  102.                         exit(RETURN_FAIL) ;
  103.                         }
  104.                 my_args->RDA_ExtHelp = HELPSTRING ;
  105.                 if (!(args = ReadArgs(TEMPLATE, opts, my_args))) {
  106.                         PrintFault(IoErr(), *argv) ;
  107.                         exit(RETURN_FAIL) ;
  108.                         }
  109.                 overwrite_files = opts[OVERWRITE] ;
  110.                 quiet = opts[QUIET] ;
  111.                 extracttext = opts[EXTRACTTEXT] ;
  112.                 if (opts[DIRECTORY])
  113.                         if (OldDir = Lock((char *) opts[DIRECTORY], SHARED_LOCK))
  114.                                 OldDir = CurrentDir(OldDir) ;
  115.                         else PrintFault(IoErr(), (char *) opts[DIRECTORY]) ;
  116.  
  117.                 argv = ((char **) opts[FILES]) - 1 ;
  118.                 }
  119.         if (!NetSupportBase)
  120.                 fprintf(stdout, "Couldn't open NetSupport.Library: Can't parse configfiles.\n");
  121.  
  122.         if (!opts[FILES]) {
  123.                 fprintf(stderr, "reading from standard input\n");
  124.                 didchat = 0;
  125.                 handleMessage(stdin, "text/plain", 0, extracttext,
  126.                         (struct boundary *) NULL) ;
  127.                 if (!didchat)
  128.                     fprintf(stdout,
  129.                             "Did not find anything to unpack from standard input\n");
  130.                 }
  131.         else {
  132.                 while (*++argv)
  133.                         if (!(file = fopen(*argv, "r")))
  134.                                 os_perror(*argv) ;
  135.                         else {
  136.                                 didchat = 0 ;
  137.                                 handleMessage(file, "text/plain", 0,
  138.                                 extracttext, (struct boundary *) NULL) ;
  139.                                 fclose(file) ;
  140.                                 if (!didchat)
  141.                                         fprintf(stdout,
  142.                                                 "Did not find anything to unpack from %s\n", *argv);
  143.                                 }
  144.                         }
  145.  
  146.         exit(0) ;
  147.         }
  148.  
  149. void
  150. warn(char *s) {
  151.     fprintf(stderr, "munpack: warning: %s\n", s);
  152. }
  153.  
  154. void
  155. chat(char *s) {
  156.         didchat = 1;
  157.         if (!quiet) fprintf(stderr, "munpack: %s\n", s);
  158.         }
  159.